<?php

include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/header.inc');
include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/comp.inc');
include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/fcs.php');
?>


<?php


if(isset($_GET['task']))
{
		if(!$link=mysql_connect("localhost",$user,$pass))
			die("Error in Database Connection");

		if(!mysql_select_db("bs"))
			echo("Error in Database Selection");

		if($_GET['task']=="displayform")
		{
				$fromdate="01-".date("m-Y");
				$todate="31-".date("m-Y");
				$arr=explode("-",$todate);
			
				while(!checkdate($arr[1],$arr[0],$arr[2]))
				{
						$arr[0]=$arr[0]-1;
				}
				$todate=$arr[0]."-".$arr[1]."-".$arr[2];
			
?>

				| <a href="display.php">Display </a> | <a href=journal_display.php?task=displayform>Journal Book</a></div>
				<h3>Select Date Range</h3>
				<table cellpadding="3" class="list">
				<form name="displayform" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
				<tr><td><label>From </label></td><td> <input type="text" name="fromdate" size="12" value="<?php echo $fromdate; ?>" /></td></tr>
				<tr><td><label>To </label></td><td> <input type="text" name="todate" size="12" value="<?php echo $todate; ?>" /></td></tr>
				<tr><td><label>Mode</label></td><td><select name="mode"><option value="general">General</option><option value="print">Print</option></select></td></tr>
		
				<tr><td align="center"><input type="submit" value="Display" align="absmiddle" /></td></tr>
				<input type="hidden" name="task" value="display" />
				</form>
				</table>
			
		
<?php
		}//END OF TASK=DISPLAY FORM
}//END OF ISSET GET

else if(isset($_POST['task']))
{
?>

		| <a href="display.php">Display </a> | <a href=journal_display.php?task=displayform>Journal Book</a></div>
		<h3>Journal Book From <font color="#FF6600"><?php echo $_POST['fromdate']; ?></font> To <font color="#FF6600"><?php echo $_POST['todate']; ?></font></h3>

<?php		

		if(!$link=mysql_connect("localhost",$user,$pass))
				die("Error in Database Connection");

		if(!mysql_select_db("bs"))
				echo("Error in Database Selection");
		

		if($_POST['task']=="display")
		{
				$valid=1;
				
				//VALIDATING DATEs
				
				if(!ereg("^[0-3]?[0-9][\./\-][0-1]?[0-9][\-]20[0-9][0-9]$",$_POST['fromdate'])) 
				{
						echo "Check Your Input 'From' Date<br>";
						$valid=0;
				}
				else
				{
					$datearr=explode("-",$_POST['fromdate']);
					if(!checkdate($datearr[1],$datearr[0],$datearr[2]))
					{
							echo "Check Your Input 'From' Date<br>";
							$valid=0;
					}
					else
							$fromdate=date("Y-m-d",mktime(0,0,0,$datearr[1],$datearr[0],$datearr[2]));
				}
				
				
				if(!ereg("^[0-3]?[0-9][\./\-][0-1]?[0-9][\-]20[0-9][0-9]$",$_POST['todate'])) 
				{
						echo "Check Your Input 'To' Date<br>";
						$valid=0;
				}
				else
				{
					$datearr=explode("-",$_POST['todate']);
					if(!checkdate($datearr[1],$datearr[0],$datearr[2]))
					{
							echo "Check Your Input 'To' Date<br>";
							$valid=0;
					}
					else
							$todate=date("Y-m-d",mktime(0,0,0,$datearr[1],$datearr[0],$datearr[2]));
				}
				
				
	
			
				if(isset($_POST['ledger']) and $_POST['ledger']!='ALL')
					$ledger=" and p.debit_ledger_id=".$_POST['ledger'];
				else
					$ledger="";
			
				if(isset($_POST['pledger']) and $_POST['pledger']!='ALL')
					$pledger=" and p.credit_ledger_id=".$_POST['pledger'];
				else
					$pledger="";
				
				
				//FILTER FORM	
?>
		
				<form name="filterform" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" id='remove_for_print_form'>
				<table class="main" cellpadding="3">
				<tr>
				<td><select name="ledger">
					<option value="ALL">To All Ledgers</option>
<?php					
				//EXTRACT ALL LEDGERS EXCEPT CASH BANK
				$resultmain=mysql_query("select * from bs_groups where under='00'");
				if(!$resultmain)
						echo("Error in Query Execution");
	
				while($arrmain=mysql_fetch_array($resultmain))
				{
						extract_ledger_ids_except_cash_bank($arrmain['id']);
						echo"Hi";
				}
				
?>				
				</select></td>
				<td><select name="pledger">
				<option value="ALL">From All Ledgers</option>
<?php					
				//EXTRACT ALL LEDGERS
				$resultmain=mysql_query("select * from bs_groups where under='00'");
				if(!$resultmain)
						echo("Error in Query Execution");
	
				while($arrmain=mysql_fetch_array($resultmain))
				{
						extract_ledger_ids_except_cash_bank($arrmain['id']);
				}
				
?>				
				</select>
				</td>
				<td><input type="submit" value="Go" /></td>
				<input type="hidden" name="task" value="display" />
				<input type="hidden" name="fromdate" value="<?php echo $_POST['fromdate'] ?>" />				
				<input type="hidden" name="todate" value="<?php echo $_POST['todate'] ?>" />	
				<input type="hidden" name="mode" value="<?php echo $_POST['mode'] ?>" />					
							
				</tr>
				</table>
				</form>
<?php				
				if($valid==1)
				{
					
								if($_POST['mode']=='general')
								{
									$result=mysql_query("select *,p.id as pid,l1.ldname as ldname,l2.ldname as pldname from bs_journal_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.debit_ledger_id=l1.id and p.credit_ledger_id=l2.id and p.date>='{$fromdate}' and p.date<='{$todate}' ".$ledger.$pledger);
								}
								else if($_POST['mode']=='print')
								{
									$result=mysql_query("select *,p.id as pid,l1.ldname as ldname,l2.ldname as pldname from bs_journal_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.debit_ledger_id=l1.id and p.credit_ledger_id=l2.id and p.date>='{$fromdate}' and p.date<='{$todate}' ".$ledger.$pledger." order by p.date");
								}

								if(!$result)
								{
										echo "Journal Book Extraction Err: ".mysql_error();
										echo "select *,p.id as pid,l1.ldname as ldname,l2.ldname as pldname from bs_journal_book as p,bs_ledgers as l1,bs_ledgers as l2 where p.debit_ledger_id=l1.id and p.credit_ledger_id=l2.id and p.date>='{$fromdate}' and p.date<='{$todate}' ".$ledger.$pledger;
								}
								else
								{
										
										echo "<table border='1' class='display_panel' cellpadding='1' >";
										echo "<tr>";if($_POST['mode']=='print')echo"<th>S No</th>";else echo"<th>Journal Id</th>";echo"<th>Date</th><th>Debit Ledger</th><th>Description</th><th>Nett Amount</th><th>Credit Ledger</th><tr>";										
										$row=1;
										while($arr=mysql_fetch_array($result))
										{
					
												//DATE CONVERSION TO DD-MM-YYYY
												$arrdate=explode("-",$arr['date']);
												$date=date("d-m-Y",mktime(0,0,0,$arrdate[1],$arrdate[2],$arrdate[0]));								
												
												//DISPLAYING
												
												echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo">";if($_POST['mode']=='print')echo"<td>{$row}</td>";else echo"<td><a href='journal_trans.php?task=edit_form&pid={$arr['pid']}'>{$arr['pid']}</a></td>";echo"<td>{$date}</td><td>{$arr['ldname']}</td><td>{$arr['description']}</td><td><div align='right'><b>{$arr['amount']}</b></div></td><td>{$arr['pldname']}</td></tr>";
												$row++;
										}
										
										//EXTRACTING Payment SUM
										$result1=mysql_query("select sum(amount) as total from bs_journal_book as p where  p.date>='{$fromdate}' and p.date<='{$todate}' ".$ledger.$pledger);
										if(!$result1)
												echo "Journal Book Extraction Err: ".mysql_error();
										else
												$arr1=mysql_fetch_array($result1);
												
										echo "<tr "; if($row%2==0)echo"bgcolor='#99CCFF'";else echo"bgcolor='#99FFFF'";  echo"><td></td>  <td></td> <td></td>  <td><b>Total</b></td>  <td><div align='right'><b>{$arr1['total']}</b></div></td>  <td>Rs</td>  </tr>";

										
										echo "</table>";
								}
								


				
				
				}//END OF IF VALID=1;

					
		}//END OF TASK=DISPLAY
}//END OF ISSET POST		

?>
